import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class ReplaceTool {
	private String from;
	private String to;
	private String replaceFrom;
	private String replaceTo;
	private String filter[];
	private String charSet = "UTF-8";
	
	public ReplaceTool(){
		
	}
	public ReplaceTool(String from,String  to , String charSet , String  filter[] , String replaceFrom , String replaceTo){
		this.from = from;
		this.to = to;
		this.filter = filter;
		this.replaceFrom = replaceFrom;
		this.charSet = charSet;
	}
	
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getReplaceFrom() {
		return replaceFrom;
	}
	public void setReplaceFrom(String replaceFrom) {
		this.replaceFrom = replaceFrom;
	}
	public String getReplaceTo() {
		return replaceTo;
	}
	public void setReplaceTo(String replaceTo) {
		this.replaceTo = replaceTo;
	}
	public String[] getFilter() {
		return filter;
	}
	public void setFilter(String[] filter) {
		this.filter = filter;
	}
	
	public String getCharSet() {
		return charSet;
	}
	public void setCharSet(String charSet) {
		this.charSet = charSet;
	}
	public List<String> replace(){
		
		List<String> collection = new ArrayList<String>();
		FileUtil.replaceFiles(from, to, charSet ,filter, replaceFrom, replaceTo, collection);
		return collection;
	}
	public static void main(String s[])
	{
		
		String from = getInput("请输入要替换的文件夹路径：");
		String to = getInput("请输入文件的输出路径：");
		String encode = getInput("请输入文件编码：");
		String filter = getInput("请输入文件格式，以\",\"分隔：");
		String regexFrom = getInput("请输入要替换的regex：");
		String regexTo = getInput("请输入替换后的regex：");
		
		if(from == null || to == null || regexFrom == null || regexTo == null || filter == null ||
				"".equals(from) || "".equals(to) || "".equals(regexFrom) || "".equals(regexTo) || "".equals(filter)){
			System.out.println("输入错误");
			if("y".equals(getInput("是否重新开始(y/n)"))){
				System.out.println("-----------------------------重新开始-------------------------------------");
				main(null);
			}
			
		}
		else{
			ReplaceTool replaceTool = new ReplaceTool();
			//replaceTool.setFrom("d:/test/from");
			//replaceTool.setTo("d:/test/to");
			//String s1[] = {"jsp","html"};
			//replaceTool.setFilter(s1);
			//replaceTool.setReplaceFrom("http://hi\\.mapbar\\.com");
			//replaceTool.setReplaceTo("http://my.mapbar.com");
			//replaceTool.setCharSet("GBK");
			
			replaceTool.setFrom(from);
			replaceTool.setTo(to);
			String s1[] = filter.split(",");
			replaceTool.setFilter(s1);
			replaceTool.setReplaceFrom(regexFrom);
			replaceTool.setReplaceTo(regexTo);
			replaceTool.setCharSet(encode);
			
			List<String> list = replaceTool.replace();
			if(list != null && list.size() > 0){
				for(int i=0;i<list.size();i++)
				{
					System.out.println("修改了："+list.get(i));
					
				}
				System.out.println("共修改了："+list.size()+"个文件");
			}
			else{
				
				System.out.println("没找到符合要求的文件");
			}
		}
	}
	public static String getInput(String title){
		System.out.println(title);
		BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
		try {
			return bReader.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}

}
